module StationManagement (printStations, printStations', removeStation, editStation, addStation, getStations, getStationsIds, addManyStations, stationStr, promptStation, promptStation', getStationById) where

import Data.Maybe
import Control.Monad
import System.IO

import DataTypes
import DataStorage
import GenericFunctions

dataFile = "stations"


-- ********************************* 
-- *  Stations managment interface *
-- *********************************

-- Function used to list stations.
printStations = getStations >>= printStations' >> pressEnter

printStations' stlist = showItems "Available stations" [stationStr s | s <- stlist]

-- Function used to remove station.
removeStation = do  stations <- getStations
                    s <- promptStation' stations
                    when (isJust s) (deleteStation (fromJust s) stations)

-- Function used to edit station.
editStation =   do  stations <- getStations
                    s <- promptStation' stations
                    when (isJust s) (changeStation (fromJust s) stations)
                    
-- Function used to add station.
addStation = do name <- promptLine "Station name: "                
                city <- promptLine "City: "
                getStations >>= newStation name city

-- Function used to add many stations
addManyStations = do choice <- promptLine "Do you want to add a new station (Y/N): "
                     if (choice /= "Y") then
                        return ()
                     else do addStation
                             addManyStations
                       
-- loads stations from its storage
getStations = DataStorage.loadItems dataFile                            

-- Function used to build a readable station description
stationStr (Station id name city)  = "\"" ++ name ++ "\" w " ++ city

-- Function used to choose a station
promptStation = getStations >>= promptStation'

promptStation' stlist = showMenu "Station choice" ([(stationStr s, return (Just s)) | s <- stlist] ++ [("[DONE]", return Nothing)])
                                    
-- Function used to get a station with a specified id                
getStationById sid = DataStorage.getItem (\(Station id _ _) -> id == sid) dataFile

-- Function used to get ids of stations which are in a collection
getStationsIds stlist = map (\(Station id _ _) -> id) stlist

-- Function serializes station and saves it to the file.
saveStation station = DataStorage.addItem station dataFile

-- Function used to filter stations with certain id. 
-- Attention: Might be changed to lambda but in this form
-- it is easier to understand.
checkStationId searchId (Station id _ _) = id /= searchId

-- Function used to prepare station object to the serialization.
newStation name city stations =  do saveStation station 
                                    where station = Station id name city
                                          id = (getNextStationId stations) + 1

-- Function checks the biggest station id in current collection
getNextStationId [] = 0
getNextStationId ((Station id _ _) : xs) = (max id (getNextStationId xs))

-- Function used to remove selected station.
deleteStation s stations  = DataStorage.saveItems (filter (\sx -> s /= sx) stations) dataFile


-- Function used to edit selected station.
changeStation s stations =  do  name <- promptLine $ "New station name [" ++ (stationName s) ++ "]"
                                city <- promptLine $ "New city name [" ++ (city s) ++ "]"
                                name <- return $ changeNull name (stationName s)
                                city <- return $ changeNull city (DataTypes.city s)
                                let (s1, s2) = break (\sx -> sx == s) stations
                                let newList = s1 ++ [Station (DataTypes.id s) name city] ++ (tail s2)
                                DataStorage.saveItems newList dataFile

